package cc.mcggdd.leah.boot.stock.controller;

import cc.mcggdd.leah.boot.stock.service.StockBillService;
import cc.mcggdd.leah.boot.stock.service.StockDebtService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cc.mcggdd.leah.boot.stock.model.form.StockDebtForm;
import cc.mcggdd.leah.boot.stock.model.query.StockDebtQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockDebtVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cc.mcggdd.leah.boot.common.result.PageResult;
import cc.mcggdd.leah.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.math.BigDecimal;

/**
 * 负债管理前端控制层
 *
 * @author 
 * @since 2025-02-10 11:15
 */
@Tag(name = "负债管理接口")
@RestController
@RequestMapping("/api/v1/stockDebts")
@RequiredArgsConstructor
public class StockDebtController  {

    private final StockDebtService stockDebtService;
    private final StockBillService stockBillService;

    @Operation(summary = "负债管理分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('stock:stockDebt:query')")
    public PageResult<StockDebtVO> getStockDebtPage(StockDebtQuery queryParams ) {
        IPage<StockDebtVO> result = stockDebtService.getStockDebtPage(queryParams);
        for (StockDebtVO vo : result.getRecords()) {
            vo.setUnsettled(stockBillService.getStockTotalAmount(vo.getId(), "N"));
        }
        return PageResult.success(result);
    }

    @Operation(summary = "新增负债管理")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('stock:stockDebt:add')")
    public Result<Void> saveStockDebt(@RequestBody @Valid StockDebtForm formData ) {
        boolean result = stockDebtService.saveStockDebt(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取负债管理表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('stock:stockDebt:edit')")
    public Result<StockDebtForm> getStockDebtForm(
        @Parameter(description = "负债管理ID") @PathVariable Long id
    ) {
        StockDebtForm formData = stockDebtService.getStockDebtFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改负债管理")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('stock:stockDebt:edit')")
    public Result<Void> updateStockDebt(
            @Parameter(description = "负债管理ID") @PathVariable Long id,
            @RequestBody @Validated StockDebtForm formData
    ) {
        boolean result = stockDebtService.updateStockDebt(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除负债管理")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('stock:stockDebt:delete')")
    public Result<Void> deleteStockDebts(
        @Parameter(description = "负债管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = stockDebtService.deleteStockDebts(ids);
        return Result.judge(result);
    }
}
